# 为Tasks模型的所有字段赋值的批量插入脚本
import sys
import random
from datetime import datetime, timedelta

# 使用正确的导入方式
from tasks.models import Tasks
print("使用tasks.models导入")

# 定义可能的数据值
epic_names = ['项目规划', '系统开发', '测试阶段']
story_names = ['用户管理', '数据统计', '报表生成', '系统优化', '性能测试']
task_types = ['开发', '测试', '设计', '文档', '部署']
statuses = ['待开始', '进行中', '已完成', '已暂停', '已取消']
projects = ['WMS系统', 'ERP系统', 'CRM系统']
versions = ['v1.0', 'v1.1', 'v2.0', 'v2.1']
managers = ['张三', '李四', '王五']
responsible_persons = ['开发者A', '开发者B', '开发者C', '测试员A', '测试员B']

# 批量插入100条数据，包含所有字段
try:
    tasks_to_create = []
    for i in range(101, 201):  # 使用新的ID范围避免冲突
        # 随机生成日期
        today = datetime.now().date()
        days_offset = random.randint(1, 30)
        plan_start = today + timedelta(days=days_offset)
        plan_end = plan_start + timedelta(days=random.randint(5, 20))
        
        # 随机决定是否有实际开始和结束日期
        has_actual = random.choice([True, False])
        if has_actual:
            actual_start = (today - timedelta(days=random.randint(1, 10))).strftime('%Y-%m-%d')
            actual_end = (today + timedelta(days=random.randint(0, 5))).strftime('%Y-%m-%d')
        else:
            actual_start = None
            actual_end = None
        
        task = Tasks(
            epic_name=random.choice(epic_names),
            story_name=random.choice(story_names),
            task_id=f'TASK{i:03d}',
            task_name=f'任务{i} - {random.choice(task_types)}任务',
            task_type=random.choice(task_types),
            project=random.choice(projects),
            fc_publish_version=random.choice(versions),
            product_manager=random.choice(managers),
            status=random.choice(statuses),
            fc_responsible_person=random.choice(responsible_persons),
            estimate_time=round(random.uniform(1.0, 20.0), 2),
            fc_plan_start_time=plan_start,
            fc_plan_end_time=plan_end,
            fc_actual_start_time=actual_start,
            fc_actual_end_time=actual_end
        )
        tasks_to_create.append(task)
    
    # 批量创建
    Tasks.objects.bulk_create(tasks_to_create)
    print(f"成功插入{len(tasks_to_create)}条完整数据，所有字段都已赋值")
except Exception as e:
    print(f"插入失败: {e}")
    sys.exit(1)